<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>news</title>
    <link rel="stylesheet" type="text/css" href="../themes/default/easyui.css">
    <link rel="stylesheet" type="text/css" href="../themes/icon.css">
    <link rel="stylesheet" type="text/css" href="../demo/demo.css">
    <script type="text/javascript" src="../jquery.min.js"></script>
    <script type="text/javascript" src="../jquery.easyui.min.js"></script>
    <script type="text/javascript" src="../jquery.serialize-object.js"></script>
    <script type="text/javascript" src="../xheditor/xheditor-1.2.2.min.js"></script>
    <script type="text/javascript" src="../xheditor/xheditor_lang/zh-cn.js"></script>
</head>
<body>
    <div class="easyui-layout" data-options="fit:true">
        <div id="p" data-options="region:'west'" title="新闻分类" style="width:150px;padding:10px">
            <ul id="tt"></ul>
        </div>
        <div data-options="region:'center'" title="新闻列表">
            <table id="dg">

            </table>
            <div id="dlg" class="easyui-dialog" title="Basic Dialog" data-options="iconCls:'icon-save', closed: true" style="width:100%;height:100%;padding:10px">
                <form id="ff" class="easyui-form" data-options="novalidate:true">
                    <div style="margin-bottom: 10px" id="userId">
                        ID:<input type="text" name="_id">
                    </div>
                    <div style="margin-bottom:10px">
                        <input class="easyui-textbox" name="title" style="width:80%" data-options="label:'标题:',required:true">
                    </div>
                    <div style="margin-bottom:10px">
                        <input class="easyui-textbox" name="categoryId" readonly id="categoryId" style="width:80%" data-options="label:'类别:',required:true">
                    </div>
                    <div style="margin-bottom:10px">
                        <input class="easyui-textbox" name="author" style="width:80%" data-options="label:'编辑:',required:true">
                    </div>
                    <div style="margin-bottom:10px">
                        内容:<textarea name="content" class="xheditor" cols="120" rows="20"></textarea>
                    </div>
                </form>
                <div style="text-align:center;padding:5px 0">
                    <a href="javascript:void(0)" class="easyui-linkbutton" onclick="submitForm()" style="width:80px">Submit</a>
                    <a href="javascript:void(0)" class="easyui-linkbutton" onclick="clearForm()" style="width:80px">Clear</a>
                </div>
            </div>
        </div>
    </div>
</body>
</html>
<script>
    $(function () {
        $('#tt').tree({
            method:"get",
            url:'http://localhost:3000/classify/list',
            onClick(node){
                var child=$('#tt').tree('getChildren',node.target);
                if(child.length>0){
                    var len=child.length;
                    var newarr = [];
                    var arr1 = [];
                    for(var i=0;i<len;i++){
                        $.ajax({
                            url:'http://localhost:3000/news/list',
                            method:'post',
                            data:{categoryId:child[i]._id},
                        }).done(function (res) {
                            var leng1 = res.rows.length;
                            for(var j = 0;j < leng1;j++){
                                newarr.push(res.rows[j]);
                            }
                        })
                    }
                    setTimeout(function () {
                        $('#dg').datagrid('loadData',newarr);
                    },400)
                }else {
                    $('#dg').datagrid('load', {
                    categoryId: node._id,
                    address: 'ho',
                    });
                    $('#dg').datagrid('reload');
                }
            },
        });
        var toolbar=[
            {
            text:'Add',
            iconCls:'icon-add',
            handler:function(){
                var selectedNode = $('#tt').tree('getSelected');
                if(selectedNode){
                    $('#dlg').dialog('open');
                    var catId=selectedNode._id;
                    console.log(catId);
                    $('#ff input').val("");
                    console.log($('#ff input'));
                    $('#ff').form('load',{
                        categoryId:catId,
                    })
                }else {
                    $.messager.alert('提示','请在左边选择新闻类型');
                }
            }
        },{
            text:'Cut',
            iconCls:'icon-cut',
            handler:function(){
                var s=$('#dg').datagrid('getSelections');
                var arr=[];
                var len=s.length;
                for(var i=0;i<len;i++){
                    arr.push(s[i]._id);
                }
                if(arr.length>0){
                    var data=arr.toString();
                    $.messager.confirm('提示', '你确定要删除吗?', function(b){
                        console.log(data);
                        if (b){
                            $.ajax({
                                url:"http://localhost:3000/news/deletes",
                                method:"post",
                                data:{idarr:data}
                            }).done(function (res) {
                                console.log(res.status);
                                if(res.status==200){
                                    $.messager.alert('提示','删除成功!');
                                }else {
                                    $.messager.alert('提示','请选择要删除的数据！','warning');
                                }
                                $('#dg').datagrid('reload');
                            })
                        }
                    });

                }else {
                    $.messager.alert('提示','请选择要删除的数据！','warning');
                }
            }
        },
            {
                text:'Search',
                iconCls:'icon-search',
                handler:function(){
                    $.messager.prompt('提示', '请输入您要搜索的内容：', function(r){
                        if (r){
                            $('#dg').datagrid('load', {
                                title: r,
                                address: 'ho',
                            });
                            $('#dg').datagrid('reload');

                        }
                    });
                }
            },
            {
                text:'刷新',
                iconCls:'icon-reload',
                handler:function(){
                    $('#dg').datagrid('load', {
                        title: '',
                        address: 'ho',
                    });
                    $('#dg').datagrid('reload');
                    $("#tt").tree("reload");
                }
            }
        ];
        $('#dg').datagrid({
            url:'http://localhost:3000/news/list',
            method:'post',
            pagination:true,
            toolbar:toolbar,
            fit:true,
            columns:[[
                {field:'ck',checkbox:true},
                {field:'title',title:'标题',width:300,align:'center'},
                {field:'categoryId',title:'类别',width:80,align:'center'},
                {field:'author',title:'编辑',width:100,align:'center'},
                {field:'releaseTime',title:'发布时间',width:100,align:'center'},
                {field:'commentCount',title:'评论数',width:100,align:'center'},
                {field:'opt',title:'操作',width:180,align:'center',
                    formatter: function(value,row,index){
                        return "<a href='javascript:void(0)' name='upd' class='easyui-linkbutton' onclick='edit(this)' data-id='"+JSON.stringify(row)+"'>修改</a>" +
                            " <a href='javascript:void(0)' name='del' class='easyui-linkbutton' onclick='delrow()'>删除</a>"+
                            " <a href='javascript:void(0)'  name='com' class='easyui-linkbutton' onclick='comment(this,event)' data-newsId='"+JSON.stringify(row)+"'>评论</a>";
                    }
                },
            ]],
            onLoadSuccess:function(data){
                $("a[name='upd']").linkbutton({text:'修改',plain:true,iconCls:'icon-edit'});
                $("a[name='del']").linkbutton({text:'删除',plain:true,iconCls:'icon-cut'});
                $("a[name='com']").linkbutton({text:'评论',plain:true,iconCls:'icon-search'});
            },

        });
    })
    function comment(ee,event) {
        event.stopPropagation();
        var data = $(ee).attr('data-newsId');
        var obj=JSON.parse(data);
        var commentUrl="comment.html?newsid="+obj._id;
        addTab("评论列表",commentUrl,null,1);
    }
    function delrow() {
        $.messager.confirm('提示', '你确定要删除吗?', function(b){
            if (b) {
                var id=$('#dg').datagrid('getSelections');
                $.ajax({
                    url: 'http://localhost:3000/news/data/' + id[0]._id,
                    method: 'delete'
                }).done(function (res) {
                    $('#dg').datagrid('reload');
                })
            }else {
                $('#dg').datagrid('clearChecked');
            }
        });
    }
    function edit(ee) {
        var obj=JSON.parse($(ee).attr('data-id'));
        $('#dlg').dialog('open');
        $('#ff').form('load',obj);
    }
    function submitForm(){
        $('#ff').form('submit',{
            onSubmit:function(){
                if($(this).form('enableValidation').form('validate')){
                    //ajax操作
                    var data=$('#ff').serializeObject();
                    if(data._id&&data._id.trim().length>0){
                        //存在，则只修改
                        $.ajax({
                            method:"put",
                            url:"http://localhost:3000/news/data/"+data._id,
                            data:data,
                        }).done(function (res) {
                            $('#dlg').dialog('close');
                            $('#dg').datagrid('reload');
                        });

                    }else {
                        //不存在，则添加，还要把当前的id给删掉
                        delete data._id;
                        $.ajax({
                            method:"post",
                            url:"http://localhost:3000/news/data",
                            data:data,
                        }).done(function (res) {
                            $('#dlg').dialog('close');
                            $('#dg').datagrid('reload');
                        });
                    }
                }else{
                    $.messager.alert('提示','请重新输入用户信息!');
                }
            }
        });
    }
    function clearForm(){
        $('#ff').form('clear');
    }
    function addTab(title, href, iconCls, iframe) {
        var tabPanel=parent.$('#center-tabs');
        //如果不存在这个tab，则添加
        if(!tabPanel.tabs('exists',title)){
            var content='<iframe scrolling="auto" frameborder="0"  src="'+ href +'" style="width:100%;height:100%;"></iframe>';
            //frames模式
            if(frames){
                tabPanel.tabs('add',{
                    title:title,
                    content:content,
                    iconCls:iconCls,
                    fit:true,
                    cls:'pd3',
                    closable:true
                });
            }else {
                tabPanel.tabs('add',{
                    title:title,
                    href:href,
                    iconCls:iconCls,
                    fit:true,
                    cls:'pd3',
                    closable:true
                });
            }
        }else {
            tabPanel.tabs('select',title);
            //刷新tabs操作
            var current_tab=tabPanel.tabs('getSelected');
            tabPanel.tabs('update',{
                tab:current_tab,
                options:{
                    content:'<iframe scrolling="auto" frameborder="0" src="'+href+'" style="width: 100%;height:100%;">'
                }
            })
        }
    }
</script>
